﻿using System.Collections.Generic;
using System.Linq;

namespace WatchDog
{
    public class FileService
    {
        public List<FileInfo> GetFileInfo() {
            using (var db = new SqliteDbContext())
            {
                return db.QueryFileInfos.ToList();
            }
        }

        public void Add(FileInfo file)
        {
            using (var db = new SqliteDbContext())
            {
                db.FileInfos.Add(file);
                db.SaveChanges();
            }
        }

        public void Del(int id)
        {
            using (var db = new SqliteDbContext())
            {
                var model = db.QueryFileInfos.FirstOrDefault(c=>c.Id == id);
                if (model != null)
                {
                    db.FileInfos.Remove(model);
                    db.SaveChanges();
                }
            }
        }

        public void Update(int id,bool auto)
        {
            using (var db = new SqliteDbContext())
            {
                var model = db.QueryFileInfos.FirstOrDefault(c => c.Id == id);
                if (model != null)
                {
                    model.AutoStart = auto; 
                    db.FileInfos.Update(model);
                    db.SaveChanges();
                }
            }
        }
    }
}
